<?php
/*
 +----------------------------------------------------------------------
 + Title        : Notify 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2019/6/27 14:45
 + Last-time    : 2019/6/27 14:45+ 86187
 + Desc         : Notify
 +----------------------------------------------------------------------
*/


namespace app\admin\behavior;


use app\api\service\PointService;
use app\common\service\MemberCouponLogService;
use app\common\service\MemberGamesLogService;
use app\common\service\MemberPointLogService;

class Notify
{
    protected $error;


    public function sign($param)
    {
        //计算签到奖励
        $sign_config = db('system_config_signin')
            ->where('start_time', '<=', date('Y-m-d H:i:s'))
            ->where('end_time', '>', date('Y-m-d H:i:s'))
            ->where('status', 1)
            ->find();


        //奖励数量判断
        if ($sign_config) {
            //获取奖励配置
            $award_config = db('system_config_signin_detail')
                ->where('date_num', $param['num'])
                ->where('signin_id', $sign_config['id'])
                ->order('id desc')
                ->find();
            $awards = explode(',', $award_config['award']);
            $award_list = [];
            foreach ($awards as $v) {
                $res = $this->$v($param['mid'], $award_config);
                $arr = $v . '执行' . ($res ? '成功' : '失败');
                array_push($award_list, $arr);
            }
            return $award_list;
        } else {
            return true;
        }
    }

    //签到积分处理
    public function point($mid, $config)
    {
        $data['mid'] = $mid;
        $member = getLocalMember($mid);
        $service = new PointService();
        $res = $service->incPoint($member ,4 ,$config['point'],$config['signin_id'] ,'','签到送积分','sign');
        return $res;
    }

    //签到游戏处理
    public function games($mid, $config)
    {
        $service = new MemberGamesLogService();
        $add['mid'] = $mid;
        $add['games_id'] = $config['games'];
        $add['scene'] = 'sign';
        $add['scene_id'] = $config['signin_id'];
        $add['num'] = 1;
        $add['surplus_num'] = 1;
        $add['status'] = 1;
        $res = $service->add($add);
        return isset($res['id']) ? true : false;
    }

    //优惠券处理
    public function coupon($mid, $config)
    {
        $service = new MemberCouponLogService();
        $add['mid'] = $mid;
        $add['coupon_id'] = $config['coupon_id'];
        $add['scene'] = 'sign';
        $add['scene_id'] = $config['signin_id'];
        $res = $service->add($add);
        return isset($res['id']) ? true : false;
    }
}